﻿using HeterogeneousTree.ProductModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HeterogeneousTree
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var prdPackageSection = BuildProductPackageSection();
            DisplayData(prdPackageSection);
            Console.ReadLine();
        }

        private static ProductPackageSection BuildProductPackageSection()
        {
            var subServiceForService1AndProduct1 = new SubService { SubServiceNames = new List<string> { "subService1ForService1AndProduct1", "subService2ForService1AndProduct1" } };
            var subServiceForService2AndProduct1 = new SubService { SubServiceNames = new List<string> { "subService1ForService2AndProduct1", "subService2ForService2AndProduct1" } };
            var subServiceForService1AndProduct2 = new SubService { SubServiceNames = new List<string> {"subService1ForService1AndProduct2", "subService2ForService1AndProduct2" }};
            var subServiceForService2AndProduct2 = new SubService { SubServiceNames = new List<string> {"subService1ForService2AndProduct2", "subService2ForService2AndProduct2" }};

            var service1ForProduct1Section = new ServiceSection { SectionName = "service1ForProduct1", Model = subServiceForService1AndProduct1 };
            var service2ForProduct1Section = new ServiceSection { SectionName = "service2ForProduct1", Model = subServiceForService2AndProduct1 };
            var service1ForProduct2Section = new ServiceSection { SectionName = "service1ForProduct2", Model = subServiceForService1AndProduct2 };
            var service2ForProduct2Section = new ServiceSection { SectionName = "service2ForProduct2", Model = subServiceForService2AndProduct2 };

            var product1Section = new ProductSection(new List<ServiceSection> { service1ForProduct1Section, service2ForProduct1Section }) { SectionName = "product1Section" };
            var product2Section = new ProductSection(new List<ServiceSection> { service1ForProduct2Section, service2ForProduct2Section }) { SectionName = "product2Section" };


            var prdPackageSection = new ProductPackageSection(new List<ProductSection> { product1Section, product2Section });
            return prdPackageSection;
        }

        private static void DisplayData<TSection>(TSection parentSection)
            where TSection : ISection
        {
            if (!String.IsNullOrEmpty(parentSection.SectionName))
            {
                Console.WriteLine(parentSection.SectionName);
            }
            var sectionModel = parentSection.GetModel();
            //May be Strategy Pattern can be implemented here to deligate the 
            //handling of Specific Models to specific classes
            if (sectionModel != null && sectionModel is SubService)
            {
                (sectionModel as SubService).SubServiceNames.ToList().ForEach(subServiceName =>
                    {
                        Console.WriteLine("        " + subServiceName);
                    }
                );
            }
            if (parentSection.InnerSections == null || parentSection.InnerSections.Count == 0)
            {
                return;
            }
            parentSection.InnerSections.ToList().ForEach(innerSection =>
                {
                    DisplayData(innerSection);
                }
            );
        }
    }
}
